<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/25
 * Time: 14:45
 */

?>

<?php

require_once "../DB_Util/sql_helper.php";

/**
 *  在sql语句里面可以使用分页查找的方式查找
 *
 *  假设要获取第三页的10条数据
 *      从第21条开始取，总共10条
 *
 *  select * from 表名 LIMIT 从哪里开始，总共取多少条
 *
 */

//1. 从前端获取 当前的页码 每页显示多少条
$currentPage = $_POST["currentPage"];

$pageCount = $_POST["pageCount"];

//算出从哪里开始获取，一共获取多少条
$offset = ($currentPage - 1) * $pageCount;

//拼接查询sql语句
$sql = "(SELECT a.id,a.title,a.created,a.`status`,u.nickname,c.`name` FROM articles a LEFT JOIN users u ON u.id = a.user_id LEFT JOIN categories c ON a.category_id = c.id) LIMIT {$offset},{$pageCount}";

//$sql = "(SELECT a.id,a.title,a.created,a.status,u.nickname,c.name FROM articles a LEFT JOIN users u ON a.user_id = u.id LEFT JOIN categories c ON a.category_id = c.id) LIMIT {$offset},{$pageCount}";

//执行sql语句，并且接收返回值
$res = query($sql);

//除了要获取这展示的数据以外，还得获取总的数据条数
$countSql = "SELECT COUNT(*) AS maxCount FROM articles";

$countRes = query($countSql);

$arr = array("code"=>200,"msg"=>"操作失败");

if ($res){
    $arr["code"] = 100;
    $arr["msg"] = "操作成功";
    $arr["data"] = $res;
    $arr["dataCount"] = $countRes[0]["maxCount"];
}

//把数据返回给前端
echo json_encode($arr,JSON_UNESCAPED_UNICODE);

?>
